//
//  XTMyStrategyViewController.m
//  BeautifulHome
//
//  Created by dlios on 15-7-9.
//  Copyright (c) 2015年 ACE. All rights reserved.
//

#import "XTMyStrategyViewController.h"
#import "XTStrategyDetailViewController.h"
#import "XTStrategy.h"
@interface XTMyStrategyViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;
@end

@implementation XTMyStrategyViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(button)];
    self.navigationController.navigationBar.translucent = NO;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageView.image = [UIImage imageNamed:@"999b"];
    [self.view addSubview:imageView];
    self.title = @"我的攻略";
    [self createTableView];
    [self get];
}
- (void)get
{
    self.arr = [NSMutableArray array];
    //归档解档收藏
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    //创建文件管理器(单例)
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //添加文件夹
    //拼接文件夹地址
    NSString *filePath = [path stringByAppendingPathComponent:@"收藏"];
    //创建文件夹
    [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *strPath = [filePath stringByAppendingPathComponent:@"myStrategy.plist"];
    self.arr = [NSKeyedUnarchiver unarchiveObjectWithFile:strPath];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
    [_tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseID = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
    }
    XTStrategy *mStrategy = [[[XTStrategy alloc]init] autorelease];
    cell.backgroundColor = [UIColor clearColor];
    mStrategy = _arr[indexPath.row];
    cell.textLabel.text = mStrategy.title;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XTStrategyDetailViewController *sdVC = [[[XTStrategyDetailViewController alloc] init] autorelease];
    XTStrategy *s1 = _arr[indexPath.row];
    sdVC.strategy = s1;
    sdVC.name = [s1.user objectForKey:@"userName"];
    sdVC.s2 = s1.photos;
    [self.navigationController pushViewController:sdVC animated:YES];
}


#pragma mark - tableView编辑方法
#warning 2.是否允许编辑 canEdit
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
#warning 4.判断编辑状态进行删除或者添加  commitEditingStyle
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //归档解档收藏
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    //创建文件管理器(单例)
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //添加文件夹
    //拼接文件夹地址
    NSString *filePath = [path stringByAppendingPathComponent:@"收藏"];
    //创建文件夹
    [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *strPath = [filePath stringByAppendingPathComponent:@"myStrategy.plist"];
    //通过editingStyle判断状态,执行删除/添加操作
    //删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSMutableArray *marr = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithFile:strPath]];
        [marr removeObjectAtIndex:indexPath.row];
        
        
        //1.删除cell之前 先从数组中对应位置 删除数据
        [_arr removeObjectAtIndex:indexPath.row];
        //2.删除的信息需要被存放在一个数组中
        NSArray *arr = @[indexPath];
        //3.让视图和数据匹配
        //参数1: 删除信息的数组
        //参数2: 删除操作的动画效果
        [_tableView deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationLeft];
        [NSKeyedArchiver archiveRootObject:marr toFile:strPath];
    }
    
}
//删除样式 title(确认删除)
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
- (void)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
